import { native, sys } from "cc";
import LocalStorage from "../engine/LocalStorage";
import { Logger } from "../engine/Logger";


const Native_Android_Class: string = "com/jhaosdk/NativeSdkHelper"
const Native_IOS_Class: string = "NativeSdkHelper"

export class NativeSdkHelper {

    public static Channel_Type = {
        Web: 0,
        Ios: 1,
        android: 2,
    }

    public static channel: number = 1;

    public static init() {
        window["NativeSdkHelper"] = NativeSdkHelper;

        // WechatSdkManager.init()
        // VoiceSdkHelper.init()
        if (NativeSdkHelper.isIos()) {
            ////保持屏幕常亮
            NativeSdkHelper.channel = NativeSdkHelper.Channel_Type.Ios
            native.reflection.callStaticMethod(Native_IOS_Class, "keyScreenLight:", "")
        } else if (NativeSdkHelper.isAndroid()) {
            NativeSdkHelper.channel = NativeSdkHelper.Channel_Type.android
            native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "setKeepScreenOn", "(Z)V", true);
        } else {
            NativeSdkHelper.channel = NativeSdkHelper.Channel_Type.Web
        }
        // setTimeout(() => {
        //     this.showFloatWindowNewWay()
        // }, 1000);
    }

    public static isAndroid() {
        if (sys.isNative) {
            if (sys.os == sys.OS.ANDROID || sys.os == sys.OS.LINUX
                || sys.os == sys.OS.OHOS || sys.os == sys.OS.OPENHARMONY) {
                return true
            }
        }
    }

    public static isIos() {
        if (sys.isNative) {
            if ((sys.os == sys.OS.IOS || sys.os == sys.OS.OSX)) {
                return true;
            }
        }
        return false;
    }

    /**
    * 原生平台copy功能
    * @param copyStr 
    */
    public static copyToClipBoard(copyStr: string) {
        if (sys.isNative) {
            native.copyTextToClipboard(copyStr)
        }
        else {
            Logger.log('复制', copyStr);
            var textArea = document.getElementById("clipBoard");
            if (textArea === null) {
                textArea = document.createElement("textarea");
                textArea.id = "clipBoard";
                textArea.textContent = copyStr;
                document.body.appendChild(textArea);
            }
            textArea.select();
            try {
                const msg = document.execCommand('copy') ? 'successful' : 'unsuccessful';
                Logger.log("已经复制到剪贴板");
                document.body.removeChild(textArea);
            } catch (err) {
                Logger.log("复制到剪贴板失败");
            }
        }
    }

    public static vibrate() {
        if (this.isAndroid()) {
            native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "vibrate", "(F)V", 1);
        }
        else if (this.isIos()) {
            native.reflection.callStaticMethod("NativeSdkHelper", "vibrate:", "");
        }
    }


    private static randomMachineId() {
        let machineId: string = LocalStorage.getString("MachineID");
        if (machineId == null) {
            var mahineIdList: Array<string> = [];
            var data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            for (var i = 0; i < 16; i++) {
                mahineIdList.push(data.charAt(Math.random() * data.length));
            }
            machineId = mahineIdList.join("");
            machineId = "H5-" + machineId;
            LocalStorage.setStrig("MachineID", machineId);
        }
        return machineId;
    }

    public static getUDID() {
        let machineId: string = "abcd"
        if (this.isAndroid()) {
            machineId = native.reflection.callStaticMethod(Native_Android_Class, "getUDID", "()Ljava/lang/String;");
        }
        else if (this.isIos()) {
            machineId = native.reflection.callStaticMethod(Native_IOS_Class, "getUDID:", "");
        } else {
            machineId = this.randomMachineId();
        }
        // Logger.info("getUDID", machineId);
        return machineId;
    }

    public static getAppVersion() {
        let appVersion: string = "1.0.0"
        if (this.isAndroid()) {
            appVersion = native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "getVersion", "()Ljava/lang/String;");
        }
        else if (this.isIos()) {
            appVersion = native.reflection.callStaticMethod(Native_IOS_Class, "getAppVersion:", "");
        }
        return appVersion;
    }

    public static getSystemVersion() {
        let sysVersion: string = "1.0.0"
        if (this.isAndroid()) {
            sysVersion = native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "getSystemVersion", "()Ljava/lang/String;");
        }
        else if (this.isIos()) {
            sysVersion = native.reflection.callStaticMethod(Native_IOS_Class, "getSystemVersion:", "");
        }
        // Logger.log("getSystemVersion", sysVersion)
        return sysVersion;
    }

    public static getBundleName() {
        let bundleName: string = "com.jhao.bundle"
        if (this.isAndroid()) {
            bundleName = native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "getPackageName", "()Ljava/lang/String;");
        }
        else if (this.isIos()) {
            bundleName = native.reflection.callStaticMethod(Native_IOS_Class, "getBundleName:", "");
        }
        // Logger.log("getBundleName", bundleName)
        return bundleName;
    }

    //获取手机型号
    public static getPhoneType() {
        let iphoneType: string = "web";
        if (this.isIos()) {
            iphoneType = native.reflection.callStaticMethod(Native_IOS_Class, "getIphoneType:", "");
        }
        else if (this.isAndroid()) {
            iphoneType = native.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxHelper", "getDeviceModel", "()Ljava/lang/String;");
        }
        // Logger.log("获取手机型号====", iphoneType);
        return iphoneType
    }

    public static pay(payInfo: string) {
        // // Logger.log("pay=", payInfo)
        // if (this.isAndroid()) {
        //     let relt: string = jsb.reflection.callStaticMethod('com/jhaosdk/PaySdkHelper', "payByThirdalipay", "(Ljava/lang/String;)Ljava/lang/String;", payInfo);
        // } else if (this.isIos()) {
        //     jsb.reflection.callStaticMethod("NativeSdkHelper", "pay:", payInfo);
        // }
    }

    public static save2PhotoAlbum(picPath: string) {
        Logger.info("JsNativeBridge.saveToNativeAlbum=", picPath);
        if (sys.isNative) {
            if (this.isIos()) {
                native.reflection.callStaticMethod("NativeSdkHelper", "save2PhotoAlbum:", picPath);
            } else if (this.isAndroid()) {
                native.reflection.callStaticMethod("org/cocos2dx/javascript/NativeSdkHelper", "save2PhotoAlbum", "(Ljava/lang/String;)V", picPath);
            }
        }
    }

    public static save2PhotoAlbumResp(msg: string) {
        Logger.log("save2PhotoAlbumResp=", msg)
    }
}

